﻿using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Youz.Common.Helper
{

    public class AppPusher
    {
        //参数设置 <-----参数需要重新设置----->
        //http的域名
        private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";

        //https的域名
        //private static String HOST = "https://api.getui.com/apiex.htm";

        //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
        private static String APPID = "EcXDKCMZoI94xCiU2uadM2";
        private static String APPKEY = "Gi9Lyfoc9DAVGvrAvpYpR4";
        private static String MASTERSECRET = "NT0iNqkadiA6nQuoIfFVJ1";

        /// <summary>
        /// 对单个用户推送
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public static string PushToSingle(string clientID, string title, string content, string payload)
        {
            if (string.IsNullOrEmpty(clientID))
                return string.Empty;

            string pushResult = string.Empty;
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
            SingleMessage message = new SingleMessage();

            message.IsOffline = true;                         // 用户当前不在线时，是否离线存储,可选
            message.OfflineExpireTime = 1000 * 3600 * 12;     // 离线有效时间，单位为毫秒，可选
            message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送，2:4G/3G/2G,1为在WIFI环境下，0为无限制环境
            if (string.IsNullOrEmpty(title))
            {
                message.Data = TransmissionTemplate(payload);
            }
            else
            {
                message.Data = NotificationTemplate(title, content, payload);
            }

            com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target();
            if (!string.IsNullOrEmpty(clientID))
            {
                target.appId = APPID;
                target.clientId = clientID;
            }

            try
            {
                pushResult = push.pushMessageToSingle(message, target);
            }
            catch (RequestException e)
            {
                pushResult = push.pushMessageToSingle(message, target, e.RequestId);
            }

            return pushResult;
        }

        /// <summary>
        /// 对单个用户推送透传
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        public static string PushToSingle(string clientID, string payload)
        {
            return PushToSingle(clientID, string.Empty, string.Empty, payload);
        }

        public static string PushToSingle(string clientID, int type)
        {
            var payload = "{\"type\":" + type.ToString() + "}";
            return PushToSingle(clientID, payload);
        }

        /// <summary>
        /// 对所有用户推送
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        public static string Push(string title, string content, string payload)
        {
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
            AppMessage message = new AppMessage();

            message.IsOffline = true;                         // 用户当前不在线时，是否离线存储,可选
            message.OfflineExpireTime = 1000 * 3600 * 12;     // 离线有效时间，单位为毫秒，可选
            if (string.IsNullOrEmpty(title))
                message.Data = TransmissionTemplate(content);
            else
                message.Data = NotificationTemplate(title, content, payload);
            //判断是否客户端是否wifi环境下推送，2:4G/3G/2G,1为在WIFI环境下，0为无限制环境
            message.PushNetWorkType = 0;

            List<String> appIdList = new List<string>();
            appIdList.Add(APPID);

            message.AppIdList = appIdList;

            return push.pushMessageToApp(message);
        }

        public static string Push(string content)
        {
            return Push(string.Empty, content, string.Empty);
        }

        /// <summary>
        /// 透传模板动作内容
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private static TransmissionTemplate TransmissionTemplate(string content)
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //应用启动类型，1：强制应用启动 2：等待应用启动
            template.TransmissionType = "2";
            //透传内容  
            template.TransmissionContent = content;

            return template;
        }

        /// <summary>
        /// 普通模板动作内容
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private static NotificationTemplate NotificationTemplate(string title, string content, string payload)
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;

            //通知栏标题
            template.Title = title;
            //通知栏内容    
            template.Text = content;
            //通知栏显示本地图片 
            //template.Logo = "";
            //通知栏显示网络图标
            //template.LogoURL = "";
            //应用启动类型，1：强制应用启动  2：等待应用启动 
            template.TransmissionType = "1";
            //透传内容
            template.TransmissionContent = payload;
            //接收到消息是否响铃，true：响铃 false：不响铃
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除
            template.IsClearable = true;
            //设置客户端展示时间
            //String begin = "2015-03-06 14:36:10";
            //String end = "2015-03-06 14:46:20";
            //template.setDuration(begin, end);            
            return template;
        }
    }
}
